
if (typeof jQuery === 'undefined') {

    throw new Error('CommonUtils.js must need Jquery plugin!!');

}

!(function () {

    var obj = {

        JSONToCSVConvertor: function(JSONData, ReportTitle, ShowLabel) {
            console.log('___',JSONData);
            var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;

            var CSV = '';

            CSV += ReportTitle + '\r\n\n';

            if (ShowLabel) {

                var row = "";

                for ( var index in arrData[0]) {

                    row += index + ',';
                }

                row = row.slice(0, -1);

                CSV += row + '\r\n';
            }

            for (var i = 0; i < arrData.length; i++) {

                var row = "";

                for ( var index in arrData[i]) {

                    row += '"' + arrData[i][index] + '",';

                }

                row.slice(0, row.length - 1);

                CSV += row + '\r\n';

            }

            if (CSV == '') {

                alert("Invalid data");

                return;

            }

            var fileName = "MyReport_";

            fileName += ReportTitle.replace(/ /g, "_");

            var uri = 'data:text/Xls;charset=utf-8,' + escape(CSV);

            var link = document.createElement("a");

            link.href = uri;

            link.style = "visibility:hidden";

            link.download = fileName + ".Xls";

            document.body.appendChild(link);

            link.click();

            document.body.removeChild(link);

        }


    };

    window.ExportUtils = obj;

})(jQuery);


